﻿using daonaite.Model.system.role;
using daonaite.services.system;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace daonaite.api.Controllers.system
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoleController : BaseController
    {
        private readonly roleService rolecz;

        public RoleController(roleService rolecz)
        {
            this.rolecz = rolecz;
        }

        /// <summary>
        /// 查询所有角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> selectall()
        {
            var list = await rolecz.selectall();
            return Success(list);
        }
        /// <summary>
        /// 根据条件查询-分页
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> select([FromBody] Dto_role data)
        {
            RefAsync<int> total = new RefAsync<int>();
            var list = await rolecz.selectpageAsync(data, total);
            return Success(list, data.pageSize, data.pageIndex, total.Value);
        }

        [HttpGet]
        public async Task<IActionResult> get(int id)
        {
            return Success( await rolecz.getAsync(id));
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> add([FromBody] Dto_role data)
        {
            sys_role role = new sys_role();
            copy<Dto_role, sys_role>(data, role, true);

            var count = await rolecz.add(role);
            return Success(count);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> update([FromBody] Dto_role data)
        {
            sys_role role = new sys_role();
            copy<Dto_role, sys_role>(data, role, false, true);

            var count = await rolecz.updateAsync(role);
            return Success(count);
        }

        [HttpGet]
        public async Task<IActionResult> delete(int id)
        {
            var count = await rolecz.delete(id);
            return Success(count);
        }

    }
}
